package com.skaz.util;

import com.skaz.Constants;
import org.springframework.core.env.Environment;

import java.util.Arrays;

/**
 * Envs
 * ---------------------------------------------------------------------------------------------------------------------
 *
 * @author YRain
 */
public class Envs {

    public static Environment ENV;

    public static String getString(String key, String defaultValue) {
        if (ENV != null) {
            String value = ENV.getProperty(key);
            if (value != null) {
                return value;
            }
        }
        return defaultValue;
    }

    public static Integer getInteger(String key, Integer defaultValue) {
        if (ENV != null) {
            Integer value = ENV.getProperty(key, Integer.class);
            if (value != null) {
                return value;
            }
        }
        return defaultValue;
    }

    public static Long getLong(String key, Long defaultValue) {
        if (ENV != null) {
            Long value = ENV.getProperty(key, Long.class);
            if (value != null) {
                return value;
            }
        }
        return defaultValue;
    }

    public static boolean getBoolean(String key, boolean defaultValue) {
        if (ENV != null) {
            Boolean value = ENV.getProperty(key, Boolean.class);
            if (value != null) {
                return value;
            }
        }
        return defaultValue;
    }

    public static String[] getActiveProfiles() {
        if (ENV != null) {
            return ENV.getActiveProfiles();
        }
        return new String[]{Constants.PROFILE_DEFAULT};
    }

    public static String getActiveProfilesString() {
        if (ENV != null) {
            String[] profiles = ENV.getActiveProfiles();
            return profiles.length == 0 ? profiles[0] : Strings.join(profiles, ",");
        }
        return Constants.PROFILE_DEFAULT;
    }

    public static boolean isDev() {
        if (ENV != null) {
            return Arrays.stream(ENV.getActiveProfiles()).anyMatch(ENV -> (ENV.equalsIgnoreCase(Constants.PROFILE_DEV)));
        }
        return true;
    }

    public static boolean isTest() {
        if (ENV != null) {
            return Arrays.stream(ENV.getActiveProfiles()).anyMatch(ENV -> (ENV.equalsIgnoreCase(Constants.PROFILE_TEST)));
        }
        return true;
    }

    public static boolean isProd() {
        if (ENV != null) {
            return Arrays.stream(ENV.getActiveProfiles()).anyMatch(ENV -> (ENV.equalsIgnoreCase(Constants.PROFILE_PROD)));
        }
        return false;
    }

    public static boolean isShow() {
        if (ENV != null) {
            return Arrays.stream(ENV.getActiveProfiles()).anyMatch(ENV -> (ENV.equalsIgnoreCase(Constants.PROFILE_SHOW)));
        }
        return false;
    }

    public static boolean isDebug() {
        return isDev() || isTest();
    }

}
